<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/******************************************************
* Profile			: 
* Author			: Administrator  
* Create Time		: 2011-10-15
* Modify Time		: 2011-10-15   
* Modify Profile	:  
******************************************************/ 
class JR_Controller extends CI_Controller
{
	public $resources = array();
	 
	public $template = "default";     

	public function get_lang()
	{
		return 
		$this->session->userdata("lang")==null?
			$this->config->item('language'):
			$this->session->userdata("lang");
	}

	public function set_lang($lang)
	{
		$this->session->set_userdata("lang", $lang);
	}

	function __construct()
    {
        parent::__construct();
       
		if($this->router->fetch_method() == 'index')
		{
			// print_r($_SERVER);
			$current_url = $_SERVER['REQUEST_URI']; 
			if(strpos(strtolower($_SERVER['SERVER_SOFTWARE']), 'iis')>0)
			{
				$current_url = $_SERVER['ORIG_PATH_INFO'] ; 
			}
			$this->session->set_userdata(CONTROL_FOLDER.'_last_index_page',$current_url); 
		} 
		
		// 加载通用的语言文件   
        $this->lang->load('form', $this->get_lang()); 
		$this->lang->load('global', $this->get_lang()); 
		$this->lang->load('seo', $this->get_lang()); 
		
		// 加载一个和控件器类同名的语言文件 
		$class_name = get_class($this);
	
		if(file_exists(APPPATH."language/".$this->get_lang()."/".$class_name."_lang.php"))
		{
			$this->lang->load(strtolower($class_name), $this->get_lang());
		} 
    } 

    public function last_index_page()  
    {
    	return $this->session->userdata(CONTROL_FOLDER.'_last_index_page'); 
    }
    
    public function goto_url($url)
    {
    	header("location:$url"); 
    }
    
    public function goto_last_index_page() 
    {
    	$this->goto_url($this->last_index_page()); 
    }
    
    public function is_postback()
    {
    	if(sizeof($_POST) == 0)
    	{
    		return false;
    	}
    	return true; 
    }
    
    
    /******************************************************
    * 注册全局的POST，主要用于在不同页面之间传递POST里的数据
    * 多用于保存查询表单的POST数据
    * 	@param unknown_type $name  为全局的POST取一个名字
    *                              同名被覆盖
    * Author			: 乔伟
    * Create Time		: 2011-10-15
    * Modify Time		: 2011-10-15
    * Modify Profile	:  
    ******************************************************/ 
    public function register_global_post($name)
    {
    	// print_r($_POST); 
    	$this->session->set_userdata($name,$_POST); 
    }
   
    /******************************************************
    * Profile			: 读取全局POST，根据名字
    * 	@param unknown_type $name
    * 	@return multitype:
    * Author			: 乔伟 
    * Create Time		: 2011-10-15
    * Modify Time		: 2011-10-15
    * Modify Profile	:  
    ******************************************************/  
    public function read_global_post($name)
    {
    	if($this->session->userdata($name) == null)
    	{
    		return Array();
    	}
    	else
    	{
    		return $this->session->userdata($name) ;
    	}
    }
    
    public function build_condition_string($global_post_name)
    {
    	$condition = "";
    	$global_post = $this->read_global_post($global_post_name); 
    	
    	if($global_post !=null)
		{
			foreach($global_post as $key => $value)
			{
				$keys = explode('_',$key);
				$k0 = $keys[0];
				$join_sign = substr($k0,0,1);
				$ope_sign = substr($k0,1,1);
				
				$column = substr($key, strlen($k0) +  1,strlen($key)-strlen($k0));
				//echo $column." ";
				$table_alias = substr($k0,2,strlen($k0)-2);
				// echo $table_alias." ";
				if($table_alias != '')
				{
					$column =  $table_alias.".".$column;
				}
				if($join_sign == 'a')
				{
					if($ope_sign=="e" && trim($value)!='')
					{
						$condition .= " and $column = '".trim($value)."'";
					}
					if($ope_sign=="l"  && trim($value)!='')
					{
						$condition .= " and $column like '%".trim($value)."%'"; 
					}
				}
			}
		}
		return $condition;
    }
    
	/******************************************************
	* Profile			: 过滤POST的数据
	*	@param $pre     前缀,仅将POST名此拥有前缀的数据加入数组 
	*   @param $post    欲过滤POST的数据
	*   @param $ignore  拥有$pre前缀，但同时拥有$ignore前缀的
	*                   将不被加入数组。例如 三个POST名
	*                   1.p_a   2.p_m_b   3.p_b  如果$ignore为空
	*                   返回的数组将拥有 a m_b b 三个键值。但如果
	*                   $ignore='p_m_' 则会忽略掉 第二个 
	* Author			: 乔伟
	* Create Time		: 2011/9/24
	* Modify Time		: 2011/9/24
	* Modify Profile	:  
	******************************************************/
	public function filter_post($post = null ,$pre="p_",$ignore = null)
	{
		$post == null && $post=$_POST; 
		$arr = Array();

		foreach($post as $key => $value)
		{
			if(is_array($value))
			{
				if(sizeof($value)==0) continue;
			}
			
			if($ignore != null)
			{
				//echo substr($key,0,strlen($ignore))." ";
				if(substr($key,0,strlen($pre)) == $pre && substr($key,0,strlen($ignore))!= $ignore)
				{
					$arr[substr($key,strlen($pre))] = $value;
				}
			}
			else 
			{
				// echo substr($key,0,strlen($pre))." "; 
				if(substr($key,0,strlen($pre)) == $pre)
				{
					if(is_array($value))
					{
						$value = implode(',',$value);
					}
					$arr[substr($key,strlen($pre))] = $value;
				}
			}
		}

		return $arr;
	} 

	/******************************************************
	* Profile			: 根据表构建类实例 ，表字段 
	*                     为实例属性
	* 	@param unknown_type $table_name  表名
	* 	@return  
	* Author			: Administrator
	* Create Time		: 2011-10-15
	* Modify Time		: 2011-10-15
	* Modify Profile	:  
	******************************************************/ 
	function build_class($table_name)
	{
		$cls = null;
		$fields = $this->db->field_data($table_name);

		foreach ($fields as $field)
		{
		    $f_name = $field->name;
			$cls->$f_name = $field->default;
		}

		return $cls ;  
	}
	
	  
	public function push_error($error_name,$error_content)
    {
    	// $this->session->set_flashdata($error_name,$error_content);
    	$this->session->set_userdata($error_name,$error_content);
    }
    
    public function pop_error($error_name)
    {
    	$value = $this->session->userdata($error_name);
    	$this->session->unset_userdata($error_name);
    	return $value;
    	//return $this->session->flashdata($error_name); 
    }
    
	public function current_user($user = 'get')
	{
		$session_name = CONTROL_FOLDER.'_current_user';
		if($user == 'get')
		{
			$current_user = $this->session->userdata($session_name);
			return $current_user;
		}
		else
		{
			// SESSION不宜放过多东西。
			//$user->ci = null;$user->db = null;$user->load = null;
			$session_user = new stdClass();
			$session_user->id = $user->id;
			$session_user->name = $user->name;
			$session_user->nickname = $user->nickname;
			$session_user->usergroup_id = $user->usergroup_id ;
			//$usergroup = $this->usergroup_manager->get($user->usergroup_id) ;
			//$session_user->usergroup = $usergroup ; 
			
			// print_r($session_user);exit; 
			$this->session->set_userdata($session_name,$session_user);
			
			// print_r($this->session->userdata($session_name,$user));
		}
	} 
	
	public function Request()
	{
		$items = $_GET;
		$arr = Array();
		foreach($items as $key=>$value)
		{
			if($key!="PHPSESSID" && $key!="ci_session")
			{
				$key = str_replace("-", ".", $key);
				$arr[$key] = $value ;   
			}
		}
		return $arr;
	}
	
}

/**
 * 后台控制器的基类 ...
 * @author 乔伟
 *
 */
class Admin_Controller extends JR_Controller
{
	function __construct()
    {
        parent::__construct();

        $site_info = $this->site_info_manager->get() ;
        
        if(!empty($site_info->global_template))
        {
        	$this->template = $site_info->global_template  ;
        }
        // 设置所有VIEW都要设置 变量
        $this->load->vars(
        	 	array('site_info'=>$site_info)
        );  
        // print_r($this->current_user());
		if($this->current_user()== null && 
		!($this->router->fetch_class()=='user' && $this->router->fetch_method()=='login') &&
		$this->router->fetch_class()!="remoter")
		{
			redirect(SELF."/user/login");   
		}
		if( ($this->router->fetch_class()=='user' && $this->router->fetch_method()=='login') ||
			($this->router->fetch_class()=='user' && $this->router->fetch_method()=='logout') ||
			($this->router->fetch_class()=='user' && $this->router->fetch_method()=='selfinfo') ||   
			($this->router->fetch_class()=='article_class' && $this->router->fetch_method()=='get_list_for_ztree') ||
			($this->router->fetch_class()=='page_class' && $this->router->fetch_method()=='get_list_for_ztree') ||
			($this->router->fetch_class()=='link_class' && $this->router->fetch_method()=='get_list_for_ztree') ||
			$this->router->fetch_class()=="remoter"
		){}
		else
		{
			$usergroup = $this->usergroup_manager->get($this->current_user()->usergroup_id);
			if($this->router->fetch_class()!="entry")
			{
				if(!$usergroup->permission($this->router->fetch_class(),$this->router->fetch_method()))
				{
					echo "have no privilege to access" ;  
					exit;  	
				} 
			}
		}
    }
}

/**
 * 前台控制器的基类 ...
 * @author 乔伟
 *
 */
class Front_Controller extends JR_Controller
{
	function __construct()
    {
        parent::__construct();

		if($this->current_user()!=null && $this->current_user()->applicant == null)
		{
			$this->current_user(null);
		} 
		
		$site_info = $this->site_info_manager->get() ;  
		
		if(!empty($site_info->global_template))
		{
			$this->template = $site_info->global_template  ; 
		}
		
		if($site_info->is_close == 1)
		{
			echo $this->load->view('front/v_close',$site_info,true);
			exit;
		}
		// 设置所有VIEW都要设置 变量   
		$this->load->vars(
			array('site_info'=>$site_info) 
		); 
    }
}  
